export function debounce(fn: any, delay = 1000, immediate = false) {
    let timer: any = null;

    return function (this: any, ...args: any[]) {
        const context = this;

        if (timer) clearTimeout(timer);

        if (immediate && !timer) {
            fn.apply(context, args);
        }

        timer = setTimeout(() => {
            timer = null;
            if (!immediate) {
                fn.apply(context, args);
            }
        }, delay);
    };
}
